package com.blogs.ui;

import java.util.ArrayList;

import com.blogs.component.BaseActivity;
import com.blogs.component.CommentAdapter;
import com.blogs.component.GetMoreListView;
import com.blogs.component.GetMoreListView.OnMoreListener;
import com.blogs.entity.CommentFeed;
import com.blogs.service.DeleteComment;
import com.blogs.service.GetCommentLine;
import com.blogs.tools.DBSharedPreferences;
import com.blogs.tools.HtmlFormat;
import com.blogs.tools.LoadingGif;
import com.cnblogs.R;

import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

public class CommentActivity extends BaseActivity implements OnItemClickListener,OnClickListener{
	enum getDataMethod{
		init,//初始化
		more,//点击更多
	}//获取数据方式
	private DBSharedPreferences sp;
	private Button title_include_back;
	private Button title_comment_write;
	private TextView title_include_title;
	private LinearLayout comment_list_body;
	private TextView comment_form_tilte;
	private GetMoreListView listView;
	private LoadingGif loadingGif;
	private ArrayList<CommentFeed> feedList;
	private CommentAdapter adapter;
	private int curpage=1;
	private boolean ismoreing=false;
	private String Title;
	private String comment_id;
	private int type;
	private String blogapp;
	private RelativeLayout title_top_bg;
	private Toast toast;
	private ProgressDialog proDdialog;
	private CommentReceiver commentReceiver;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.comment_list);
		sp= new DBSharedPreferences(this);
		ViewGroup headview=(ViewGroup)findViewById(R.id.main_title_include);
		title_top_bg=(RelativeLayout)headview;
		title_include_title=(TextView)headview.findViewById(R.id.title_include_title);
		title_include_back=(Button)headview.findViewById(R.id.title_include_back);
		title_comment_write=(Button)headview.findViewById(R.id.title_comment_write);
		title_include_title.setText("评论");
		title_include_back.setVisibility(View.VISIBLE);
		title_include_back.setOnClickListener(this);
		title_comment_write.setVisibility(View.VISIBLE);
		title_comment_write.setOnClickListener(this);
		Title=getIntent().getStringExtra("Title");
		comment_id= getIntent().getStringExtra("comment_id");
		type=getIntent().getIntExtra("type", 0);
		blogapp=getIntent().getStringExtra("blogapp");
		
		comment_list_body=(LinearLayout)findViewById(R.id.comment_list_body);
		comment_form_tilte=(TextView)findViewById(R.id.comment_form_tilte);
		comment_form_tilte.setText(Title);
		listView=new GetMoreListView(this);
		listView.setonMoreListener(new OnMoreListener() {
			
			public void onGetMore() {
				// TODO Auto-generated method stub
				if(!ismoreing)
				{
					ismoreing=true;
					GetMoreData();
				}
			}
		});
		listView.setOnItemClickListener(this);
		loadingGif=new LoadingGif(this, listView, comment_list_body,sp);
		loadingGif.setOnReloadListener(new LoadingGif.OnReloadListener() {
			
			public void onClick() {
				// TODO Auto-generated method stub
				requestData();
			}
		});
		this.requestData();
		
		commentReceiver=new CommentReceiver();//设置广播接收
		IntentFilter skinFilter = new IntentFilter();
        skinFilter.addAction("CommentReceiver");
        registerReceiver(commentReceiver, skinFilter);
        
        this.InitSkin();
		this.InitProDailog();
	}
	private void InitProDailog()
	{
		proDdialog = new ProgressDialog(this);
		proDdialog.setIndeterminate(true);
		proDdialog.setCancelable(true);
		proDdialog.setMessage("正在删除...");
	}
	private void InitSkin()
	{
		if(sp.GetIsDarkState())
		{
			title_top_bg.getBackground().setLevel(2);
			comment_list_body.getBackground().setLevel(2);
		}
		else
		{
			title_top_bg.getBackground().setLevel(1);
			comment_list_body.getBackground().setLevel(1);
		}
	}
	private void requestData()
	{
		loadingGif.showLoading();
		this.sendTimeLine(getDataMethod.init);
	}
	private void GetMoreData()
	{
		curpage++;
		this.sendTimeLine(getDataMethod.more);
	}
	public void updateRequestData(getDataMethod gdm)
	{
		if(adapter!=null)
		{
			adapter.notifyDataSetChanged();
			if(gdm==getDataMethod.init)
			{
				listView.setAdapter(adapter);
			}
		}
		else
		{
			adapter=new CommentAdapter(this, feedList);
			listView.setAdapter(adapter);
		}

		loadingGif.showData();
	}
	private void sendTimeLine(final getDataMethod gdm)
	{
		new GetCommentLine(type,comment_id,curpage,new GetCommentLine.IMsgCallBack() {
			
			public void onSuccess(ArrayList<CommentFeed> feeds) {
				// TODO Auto-generated method stub
				if(feedList==null)
					feedList=new ArrayList<CommentFeed>();
				if(gdm==getDataMethod.init)
				{
					feedList.clear();
				}
				for(int i=0;i<feeds.size();i++)
				{
					feedList.add(feeds.get(i));
				}
				if(gdm==getDataMethod.more)
				{
					ismoreing=false;
					listView.onMoreComplete();
				}
				updateRequestData(gdm);
			}
			
			public void onFailed() {
				// TODO Auto-generated method stub
				loadingGif.showFiale();
			}
		}).loadData();
	}
	public void onItemClick(AdapterView<?> parent, View view,final int position,
			long id) {
		// TODO Auto-generated method stub
		if(position>feedList.size())
			return;
		String[] items = { "回复", "引用", "取消" };
		String[] items_d = { "回复", "引用", "删除", "取消" };
		final String[] f_items;
		if(!sp.GetNickName().equals("")&&sp.GetNickName().equals(feedList.get(position).author)&&!feedList.get(position).id.equals("0"))
		{
			f_items=items_d;
		}
		else
		{
			f_items =items;
		}
		
		new AlertDialog.Builder(CommentActivity.this).setItems(f_items, new DialogInterface.OnClickListener() {

			public void onClick(DialogInterface dialog, int which) {
				// TODO Auto-generated method stub
				if(f_items[which].equals("回复"))
				{
					Intent hintent=new Intent();
					hintent.setClass(CommentActivity.this, NewComment.class);
					hintent.putExtra("comment_id", comment_id);
					hintent.putExtra("parentCommentID", feedList.get(position).id);
					hintent.putExtra("Author", feedList.get(position).author);
					hintent.putExtra("type",type);
					startActivity(hintent);
				}
				else if(f_items[which].equals("引用"))
				{
					Intent yintent=new Intent();
					yintent.setClass(CommentActivity.this, NewComment.class);
					yintent.putExtra("comment_id", comment_id);
					yintent.putExtra("parentCommentID", feedList.get(position).id);
					yintent.putExtra("Author", feedList.get(position).author);
					String com_con=feedList.get(position).content;
					com_con=HtmlFormat.HtmlToText(com_con);
					com_con=HtmlFormat.RemoveHtmlTag(com_con);
					yintent.putExtra("root",com_con);
					yintent.putExtra("type",type);
					startActivity(yintent);
				}
				else if(f_items[which].equals("删除"))
				{
					new AlertDialog.Builder(CommentActivity.this)  
					.setTitle("删除评论") 
					.setMessage("是否删除该评论？") 
					.setPositiveButton("确定", new DialogInterface.OnClickListener() {
						
						public void onClick(DialogInterface dialog, int which) {
							// TODO Auto-generated method stub
							proDdialog.show();
							new DeleteComment(sp.GetSessionID(), feedList.get(position).id, new DeleteComment.IMsgCallBack() {
								
								public void onSuccess() {
									// TODO Auto-generated method stub
									showToast("删除评论成功");
									feedList.remove(position);
									adapter.notifyDataSetChanged();
									proDdialog.hide();
								}
								
								public void onFailed(String err) {
									// TODO Auto-generated method stub
									showToast("删除评论失败,"+err);
									proDdialog.hide();
								}
							}).Del();
						}
					}) 
					.setNegativeButton("取消", null) 
					.show();
				}
				else
				{
					
				}
				
				
			}
		}).create().show();
	}
	public void onClick(View v) {
		// TODO Auto-generated method stub
		if(v==title_include_back)
		{
			finish();
		}
		else if(v==title_comment_write)
		{
			Intent intent=new Intent();
			intent.setClass(CommentActivity.this, NewComment.class);
			intent.putExtra("comment_id", comment_id);
			intent.putExtra("parentCommentID", "0");
			intent.putExtra("Author",sp.GetNickName());
			intent.putExtra("type",type);
			intent.putExtra("blogapp", blogapp);
			startActivity(intent);
		}
	}
	private void showToast(String text)
	{
		if (toast == null) {
			toast = Toast.makeText(getApplicationContext(), text, Toast.LENGTH_SHORT);
		} else {
			toast.setText(text);
			toast.setDuration(Toast.LENGTH_SHORT);
		}
		toast.show();
	}
	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
		unregisterReceiver(commentReceiver);  
	}
	class CommentReceiver extends BroadcastReceiver
	{
		@Override
		public void onReceive(Context context, Intent intent) {
			// TODO Auto-generated method stub
			int com_id=intent.getIntExtra("commentId", 0);
			if(!comment_id.equals(com_id+""))
				return;
			CommentFeed feed=new CommentFeed();
			feed.id=intent.getIntExtra("id", 0)+"";
			feed.author=intent.getStringExtra("author");
			feed.author_url=intent.getStringExtra("author_url");
			feed.comment_time=intent.getStringExtra("comment_time");
			feed.content=intent.getStringExtra("content");
			feedList.add(0, feed);
			updateRequestData(getDataMethod.more);
			listView.setSelection(0);
		}
	}
}
